﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace $safeprojectname$
{
    public enum AdjustListViewType
    {
        Default,
        Fieldset,
        DefinitionList
    }

    public class AdjustListView : VisualControl<IFilterModel<IFilter>>
    {
        /// <summary>
        ///     Allows to append CSS class name for the wrapping DIV element
        ///     (Example: "w51p" for taking 51% of row width)
        /// </summary>
        public virtual string CssClassName { get; set; }

        public virtual AdjustListViewType Type { get; set; }

        #region protected
        /// <summary>
        ///     A bit larger style for 100% width,
        ///     smaller for less then 100% - to not break the line.
        ///     Test and adjust if needed.
        /// </summary>
        /// <returns></returns>
        protected virtual string MaxRowsWidth()
        {
            if (CssClassName.IsNotEmpty()
             && !CssClassName.Equals("w100p", StringComparison.Ordinal))
            {
                return "w29p";
            }
            return "w30p";
        }
        #endregion protected

        #region Items
        protected virtual TextOrInput GetMaxRows()
        {
            return new TextOrInput(maxLength: 4)
                .SetSourceProperty(() => Model.Filter.TakeRows);
        }

        protected virtual ComboBox GetTargetType()
        {
            return new ComboBox()
                .SetInputName(Constants.WebControls.ListView.TargetMasterPageForLink)
                .SetDataSource(Model.Filter.MasterPageOptionsList)
                .SetDataValueField("key")
                .SetDataTextField("value")
                .SetSelectedValue(Model.Filter.TargetMasterPageForLink);
        }

        protected virtual TextOrInput GetTargetName()
        {
            return new TextOrInput(maxLength: 20)
                .SetSourceProperty(() => Model.Filter.TargetNameForLink);
        }
        #endregion Items

        protected override void BuildControls()
        {
            base.BuildControls();

            if (Type == AdjustListViewType.Fieldset
                || Type == AdjustListViewType.DefinitionList)
            {
                // Only DL
                VisualControl<ICoreModel> result = new DefinitionList { GetMaxRows(), GetTargetType(), GetTargetName(), };
                if (Type == AdjustListViewType.Fieldset)
                {
                    // DL wrapped with Fieldset
                    result = new Fieldset(CssClassName, Constants.Common.Description) { result };
                }

                Controls.Add(result);

                return;
            }

            var header = new H5
            {
                new Literal(GetLocalized(Constants.WebControls.ListView.ListViewAdjustments))
            };

            var controls = new PlaceHolder
            {
                new Fieldset("w32p mh30", Constants.WebControls.ListView.MaxRowsPerPage)
                {
                    new DefinitionList(PercentForLabel.p10) {GetMaxRows().AsDefinitionListItem(" ", false),}
                },
                new Fieldset("w32p mh30", Constants.WebControls.ListView.TargetMasterPageForLink)
                {
                    new DefinitionList(PercentForLabel.p10) {GetTargetType().AsDefinitionListItem(" ", false),}
                },
                new Fieldset("w32p mh30", Constants.WebControls.ListView.TargetForDetail)
                {
                    new DefinitionList(PercentForLabel.p10) {GetTargetName().AsDefinitionListItem(" ", false),}
                }
            };

            if (CssClassName.IsNotEmpty()
                && !CssClassName.Equals("w100p", StringComparison.Ordinal))
            {
                Controls.Add(new Div(CssClassName) { header, controls });
            }
            else
            {
                Controls.Add(new PlaceHolder { header, controls });
            }
        }
    }
}